package com.jiadakeji.ppm.modules.business.service;

import com.jiadakeji.ppm.common.service.BaseService;
import com.jiadakeji.ppm.common.utils.DateUtils;
import com.jiadakeji.ppm.common.utils.DecimalUtils;
import com.jiadakeji.ppm.modules.business.dao.FeeMonthDao;
import com.jiadakeji.ppm.modules.business.dao.ReportDao;
import com.jiadakeji.ppm.modules.business.entity.DTO.*;
import com.jiadakeji.ppm.modules.business.entity.FeeCharge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.List;

@Service
public class ReportService extends BaseService {

    @Autowired
    private ReportDao reportDao;
    @Autowired
    private FeeMonthDao feeMonthDao;


    public List<ReportCommunityDTO>getReportCommunity(ReportCommunityDTO reportCommunityDTO){

        LocalDate now = DateUtils.tranDateToLocalDate(feeMonthDao.get(reportCommunityDTO.getFeeMonthId()).getCurrentMonth());
        LocalDate beginDate = now.with(TemporalAdjusters.firstDayOfYear());
        LocalDate endDate = now.with(TemporalAdjusters.lastDayOfYear());
        List<ReportCommunityDTO> month = reportDao.sumByCommunity(reportCommunityDTO);

        reportCommunityDTO.setBeginDate(DateUtils.tranLocalDateToDate(beginDate));
        reportCommunityDTO.setEndDate(DateUtils.tranLocalDateToDate(endDate));
        List<ReportCommunityDTO> year = reportDao.sumByCommunityByDate(reportCommunityDTO);

        //reportCommunityDTO.setBeginDate(new Date(946656000L));
       // List<ReportCommunityDTO> history = reportDao.sumByCommunityByDate(reportCommunityDTO);
        ReportCommunityDTO rcd = new ReportCommunityDTO();
        rcd.setUsedRate(0.0);
        rcd.setReceivedRate(0.0);
        rcd.setCommunityId("0");
        rcd.setCommunityName("总计");
        for (ReportCommunityDTO m :month) {
            for (ReportCommunityDTO y:year) {
                if(y.getCommunityId().equals(m.getCommunityId())){
                    m.setYearManageFee(y.getYearManageFee());
                    m.setYearAvgElecFee(y.getYearAvgElecFee());
                    m.setYearAvgWaterFee(y.getYearAvgWaterFee());
                    m.setYearSecondPushElecFee(y.getYearSecondPushElecFee());
                    m.setYearCarMonthFee(y.getYearCarMonthFee());
                    m.setYearElevatorFee(y.getYearElevatorFee());
                    m.setYearReceivedFee(y.getYearReceivedFee());
                    m.setYearTotalFee(y.getYearTotalFee());
                    break;
                }
            }

            rcd.setUsedRoom((rcd.getUsedRoom() == null ? 0 : rcd.getUsedRoom())+m.getUsedRoom());
            rcd.setTotalRoom((rcd.getTotalRoom() == null ? 0 : rcd.getTotalRoom())+ m.getTotalRoom());
            rcd.setManageFee(DecimalUtils.add(rcd.getManageFee() == null ? 0.0 : rcd.getManageFee(),m.getManageFee()));
            rcd.setAvgElecFee(DecimalUtils.add(rcd.getAvgElecFee() == null ? 0.0 : rcd.getAvgElecFee(),m.getAvgElecFee()));
            rcd.setAvgWaterFee(DecimalUtils.add(rcd.getAvgWaterFee() == null ? 0.0 : rcd.getAvgWaterFee(),m.getAvgWaterFee()));
            rcd.setSecondPushElecFee(DecimalUtils.add(rcd.getSecondPushElecFee() == null ? 0.0 : rcd.getSecondPushElecFee(),m.getSecondPushElecFee()));
            rcd.setCarMonthFee(DecimalUtils.add(rcd.getCarMonthFee() == null ? 0.0 : rcd.getCarMonthFee(),m.getCarMonthFee()));
            rcd.setElevatorFee(DecimalUtils.add(rcd.getElevatorFee() == null ? 0.0 : rcd.getElevatorFee(),m.getElevatorFee()));
            rcd.setReceivedFee(DecimalUtils.add(rcd.getReceivedFee() == null ? 0.0 : rcd.getReceivedFee(),m.getReceivedFee()));
            rcd.setTotalFee(DecimalUtils.add(rcd.getTotalFee() == null ? 0.0 : rcd.getTotalFee(),m.getTotalFee()));
            rcd.setYearManageFee(DecimalUtils.add(rcd.getYearManageFee() == null ? 0.0 :rcd.getYearManageFee(),m.getYearManageFee()));
            rcd.setYearAvgElecFee(DecimalUtils.add(rcd.getYearAvgElecFee() == null ? 0.0 : rcd.getYearAvgElecFee(),m.getYearAvgElecFee()));
            rcd.setYearAvgWaterFee(DecimalUtils.add(rcd.getYearAvgWaterFee() == null ? 0.0 : rcd.getYearAvgWaterFee(),m.getYearAvgWaterFee()));
            rcd.setYearSecondPushElecFee(DecimalUtils.add(rcd.getYearSecondPushElecFee() == null ? 0.0 : rcd.getYearSecondPushElecFee(),m.getYearSecondPushElecFee()));
            rcd.setYearCarMonthFee(DecimalUtils.add(rcd.getYearCarMonthFee() == null ? 0.0 : rcd.getYearCarMonthFee(),m.getYearCarMonthFee()));
            rcd.setYearElevatorFee(DecimalUtils.add(rcd.getYearElevatorFee() == null ? 0.0 : rcd.getYearElevatorFee(),m.getYearElevatorFee()));
            rcd.setYearReceivedFee(DecimalUtils.add(rcd.getYearReceivedFee() == null ? 0.0 : rcd.getYearReceivedFee(),m.getYearReceivedFee()));
            rcd.setYearTotalFee(DecimalUtils.add(rcd.getYearTotalFee() == null ? 0.0 : rcd.getYearTotalFee(),m.getYearTotalFee()));
            /*for (ReportCommunityDTO h:history){
                if(h.getCommunityId().equals(m.getCommunityId())){
                    m.setHistoryReceivedFee(h.getYearReceivedFee());
                    m.setHistoryTotalFee(h.getYearTotalFee());
                    break;
                }
            }*/

        }
        month.add(rcd);

        return month;
    }

    public List<ReportBuildingDTO> getReportBuilding(ReportBuildingDTO reportBuildingDTO){

        LocalDate now = DateUtils.tranDateToLocalDate(feeMonthDao.get(reportBuildingDTO.getFeeMonthId()).getCurrentMonth());
        LocalDate beginDate = now.with(TemporalAdjusters.firstDayOfYear());
        LocalDate endDate = now.with(TemporalAdjusters.lastDayOfYear());
        List<ReportBuildingDTO> month = reportDao.sumByBuilding(reportBuildingDTO);
        reportBuildingDTO.setBeginDate(DateUtils.tranLocalDateToDate(beginDate));
        reportBuildingDTO.setEndDate(DateUtils.tranLocalDateToDate(endDate));
        List<ReportBuildingDTO> year = reportDao.sumByBuildingByDate(reportBuildingDTO);

      /*  reportBuildingDTO.setBeginDate(new Date(946656000L));
        List<ReportBuildingDTO> history = reportDao.sumByBuildingByDate(reportBuildingDTO);*/
        ReportBuildingDTO rcd = new ReportBuildingDTO();
        rcd.setUsedRate(0.0);
        rcd.setReceivedRate(0.0);
        rcd.setBuildingId("0");
        rcd.setBuildingName("总计");
        for (ReportBuildingDTO m :month) {
            for (ReportBuildingDTO y:year) {
                if(y.getBuildingId().equals(m.getBuildingId())){
                    m.setYearManageFee(y.getYearManageFee());
                    m.setYearAvgElecFee(y.getYearAvgElecFee());
                    m.setYearAvgWaterFee(y.getYearAvgWaterFee());
                    m.setYearSecondPushElecFee(y.getYearSecondPushElecFee());
                    m.setYearCarMonthFee(y.getYearCarMonthFee());
                    m.setYearElevatorFee(y.getYearElevatorFee());
                    m.setYearReceivedFee(y.getYearReceivedFee());
                    m.setYearTotalFee(y.getYearTotalFee());
                    break;
                }
            }
            rcd.setUsedRoom((rcd.getUsedRoom() == null ? 0 : rcd.getUsedRoom())+m.getUsedRoom());
            rcd.setTotalRoom((rcd.getTotalRoom() == null ? 0 : rcd.getTotalRoom())+ m.getTotalRoom());
            rcd.setManageFee(DecimalUtils.add(rcd.getManageFee() == null ? 0.0 : rcd.getManageFee(),m.getManageFee()));
            rcd.setAvgElecFee(DecimalUtils.add(rcd.getAvgElecFee() == null ? 0.0 : rcd.getAvgElecFee(),m.getAvgElecFee()));
            rcd.setAvgWaterFee(DecimalUtils.add(rcd.getAvgWaterFee() == null ? 0.0 : rcd.getAvgWaterFee(),m.getAvgWaterFee()));
            rcd.setSecondPushElecFee(DecimalUtils.add(rcd.getSecondPushElecFee() == null ? 0.0 : rcd.getSecondPushElecFee(),m.getSecondPushElecFee()));
            rcd.setCarMonthFee(DecimalUtils.add(rcd.getCarMonthFee() == null ? 0.0 : rcd.getCarMonthFee(),m.getCarMonthFee()));
            rcd.setElevatorFee(DecimalUtils.add(rcd.getElevatorFee() == null ? 0.0 : rcd.getElevatorFee(),m.getElevatorFee()));
            rcd.setReceivedFee(DecimalUtils.add(rcd.getReceivedFee() == null ? 0.0 : rcd.getReceivedFee(),m.getReceivedFee()));
            rcd.setTotalFee(DecimalUtils.add(rcd.getTotalFee() == null ? 0.0 : rcd.getTotalFee(),m.getTotalFee()));
            rcd.setYearManageFee(DecimalUtils.add(rcd.getYearManageFee() == null ? 0.0 :rcd.getYearManageFee(),m.getYearManageFee()));
            rcd.setYearAvgElecFee(DecimalUtils.add(rcd.getYearAvgElecFee() == null ? 0.0 : rcd.getYearAvgElecFee(),m.getYearAvgElecFee()));
            rcd.setYearAvgWaterFee(DecimalUtils.add(rcd.getYearAvgWaterFee() == null ? 0.0 : rcd.getYearAvgWaterFee(),m.getYearAvgWaterFee()));
            rcd.setYearSecondPushElecFee(DecimalUtils.add(rcd.getYearSecondPushElecFee() == null ? 0.0 : rcd.getYearSecondPushElecFee(),m.getYearSecondPushElecFee()));
            rcd.setYearCarMonthFee(DecimalUtils.add(rcd.getYearCarMonthFee() == null ? 0.0 : rcd.getYearCarMonthFee(),m.getYearCarMonthFee()));
            rcd.setYearElevatorFee(DecimalUtils.add(rcd.getYearElevatorFee() == null ? 0.0 : rcd.getYearElevatorFee(),m.getYearElevatorFee()));
            rcd.setYearReceivedFee(DecimalUtils.add(rcd.getYearReceivedFee() == null ? 0.0 : rcd.getYearReceivedFee(),m.getYearReceivedFee()));
            rcd.setYearTotalFee(DecimalUtils.add(rcd.getYearTotalFee() == null ? 0.0 : rcd.getYearTotalFee(),m.getYearTotalFee()));
           /* for (ReportBuildingDTO h:history){
                if(h.getBuildingId().equals(m.getBuildingId())){
                    m.setHistoryReceivedFee(h.getYearReceivedFee());
                    m.setHistoryTotalFee(h.getYearTotalFee());
                    break;
                }
            }*/

        }
        month.add(rcd);
        return month;
    }

    public FeeRecycleDTO getReportFeeRecycle(FeeRecycleDTO feeRecycleDTO){

       LocalDate now =  DateUtils.tranDateToLocalDate(feeMonthDao.get(feeRecycleDTO.getFeeMonthId()).getCurrentMonth());
       LocalDate lastYear = now.minusYears(1);
       LocalDate lastYearLastDate = lastYear.with(TemporalAdjusters.lastDayOfYear());
       LocalDate lastYearFirstDate = lastYear.with(TemporalAdjusters.firstDayOfYear());

       Date lastYearLast = new Date(946656000L);
       Date lastYearFirst = DateUtils.tranLocalDateToDate(lastYearLastDate);

       Date thisYearFirst = DateUtils.tranLocalDateToDate(now.with(TemporalAdjusters.firstDayOfYear()));
       Date thisYearLast = DateUtils.tranLocalDateToDate(now.with(TemporalAdjusters.lastDayOfYear()));

       Date thisMonthFirst = DateUtils.tranLocalDateToDate(now.with(TemporalAdjusters.firstDayOfMonth()));
       Date thisMonthLast = DateUtils.tranLocalDateToDate(now.with(TemporalAdjusters.lastDayOfMonth()));

       Date nowDate = DateUtils.tranLocalDateToDate(now);
        ReportTotalDTO reportTotalDTO = new ReportTotalDTO();
        reportTotalDTO.setBeginDate(lastYearLast);
        reportTotalDTO.setEndDate(lastYearFirst);
        reportTotalDTO.setNoStatus(FeeCharge.STATUS_FINISH);
        /**
         * 本年前累计欠费
         */
        Double preArrearFee = reportDao.sumTotalFee(reportTotalDTO);
        preArrearFee = (null == preArrearFee ) ? 0 :preArrearFee;
        reportTotalDTO.setNowDate(thisYearFirst);
        reportTotalDTO.setStatus(FeeCharge.STATUS_FINISH);
        reportTotalDTO.setNoStatus(null);
        /**
         * 之前欠费的今年回收的数量
         */
        Double lastRecycleNow =  reportDao.sumFeeReceived(reportTotalDTO);
        lastRecycleNow = (null == lastRecycleNow) ? 0:lastRecycleNow;
        /**
         *目前位置之前欠费的剩余数量
         */
        Double lastArrearsFee = DecimalUtils.add(preArrearFee,lastRecycleNow);

        reportTotalDTO.setNoStatus(FeeCharge.STATUS_FINISH);
        reportTotalDTO.setStatus(null);
        reportTotalDTO.setBeginDate(thisYearFirst);
        reportTotalDTO.setEndDate(thisYearLast);
        Double nowArrearFee = reportDao.sumTotalFee(reportTotalDTO);
        nowArrearFee = (null == nowArrearFee) ? 0 : nowArrearFee;

        reportTotalDTO.setBeginDate(thisYearFirst);
        reportTotalDTO.setStatus(null);
        reportTotalDTO.setNoStatus(null);
        Double totalFeeCharge = reportDao.sumTotalFee(reportTotalDTO);

        totalFeeCharge = (null == totalFeeCharge) ? 0 : totalFeeCharge;

        reportTotalDTO.setBeginDate(thisMonthFirst);
        reportTotalDTO.setEndDate(thisMonthLast);
        Double thisMonthFeeCharge = reportDao.sumTotalFee(reportTotalDTO);

        reportTotalDTO.setStatus(FeeCharge.STATUS_FINISH);
        Double thisMonthFeeReceived = reportDao.sumTotalFee(reportTotalDTO);
        thisMonthFeeReceived = (null == thisMonthFeeReceived) ? 0.0:thisMonthFeeReceived;
        FeeRecycleDTO frd = new FeeRecycleDTO();
        frd.setLastArrearsFee(lastArrearsFee);
        frd.setLastRecycleNow(lastRecycleNow);
        frd.setPreArrearFee(preArrearFee);
        frd.setNowArrearsFee(nowArrearFee);
        frd.setTotalArrearsFee(DecimalUtils.add(frd.getPreArrearFee(),frd.getNowArrearsFee()));
        frd.setTotalFeeCharge(totalFeeCharge);
        frd.setPreFeeReceive(DecimalUtils.subtract(totalFeeCharge,nowArrearFee));
        frd.setNowFeeCharge(thisMonthFeeCharge);
        frd.setNowFeeReceive(thisMonthFeeReceived);
        frd.setNowRecycleRate(DecimalUtils.divideGetString(frd.getNowFeeReceive(),frd.getNowFeeCharge()));
        frd.setTotalRecycleRate(DecimalUtils.divideGetString(frd.getPreFeeReceive(),frd.getTotalFeeCharge()));
        frd.setLastRecycleRate(frd.getLastArrearsFee().equals(0.0) ? "0" : DecimalUtils.divideGetString(frd.getLastRecycleNow(),frd.getLastArrearsFee()));

        return frd;
    }


}
